package com.tonysong.oa.yeboa12.config.security;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tonysong.oa.yeboa12.config.util.R;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@Component
public class RestAuthorizationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter printWriter = response.getWriter();
        //401:未登录  403：权限 404：资源不存在 405：请求错误get post
        printWriter.write(new ObjectMapper().writeValueAsString(new R(401,false,"未登录，请登录")));
        printWriter.close();
    }
}
